<?php
#### PAGES CONTROLLER #######################################################
class PagesController {

	#### CONSTRUCTOR ###########################################################
	function PagesController() {
		Base::getThis();

		// Set the page title
		$this->page_title = _('Pages');
		if (url(2)) $this->page_title .= ' '.ucwords(url(2));

		// Make database methods ready for use
		$this->model = $this->load->model('pages');

		// Perform default function
		invoke_method($this, url(2));
	}

	#### MAIN ##################################################################
	function main() {
		if (url(2)) $this->view();
		else $this->list_pages();
	}

	#### LIST ##################################################################
	function list_pages() {

		$this->body = NULL;

		$pages = $this->model->getEnabledPages();

		$this->body .= '<ul>';
		if ($pages) foreach ($pages as $page) {

			$this->body .= '<li><a href="'.url(1).'/'.$page['sef_title'].'">'.$page['title'].'</a></li>';
		}
		$this->body .= '</ul>';

		$this->output = box(_('Pages'), $this->body);
	}

	#### VIEW ##################################################################
	function view() {

		$item = $this->model->getPageBySef(url(2));

		if ($item['disabled']) $body = notice(_('Sorry, this page has been disabled.'), 'info', TRUE);
		else if (!allowed($item['visible_to'])) $body = notice(_('Sorry, you do not have permission to view this page.'), 'info', TRUE);
		else {
			$this->page_title = $item['title'];
			$body = nl2br($item['body']);
			$this->config['description'] = $item['description'];
			$this->config['keywords'] = $item['keywords'];
			if (permission('pages_manage')) $body.'<br /><br /><a href="manage/'.url(1).'/edit/'.$item['id'].'">'.('Edit Page').'</a>';
		}

		$this->output = box($item['title'], $body);
	}
}
?>